﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEXA.DataContracts;

namespace CLEXA.AdminClient.ViewModel
{
    internal sealed class UserEditVM : VMBase
    {
        private readonly IFacade facade;
        private readonly int userId;
        private string username;
        private bool isActive;
        private ICommand ok;
        private ICommand cancel;
        private ICommand apply;
        private ICommand changePassword;

        public int UserId
        {
            get { return userId; }
        }

        public string Username
        {
            get { return username; }
            set
            {
                if (username != value)
                {
                    username = value;
                    RaisePropertyChanged("Username");
                }
            }
        }

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive != value)
                {
                    isActive = value;
                    RaisePropertyChanged("IsActive");
                }
            }
        }

        public ICommand OK
        {
            get
            {
                if (ok == null)
                {
                    ok = new RelayCommand(DoOK);
                }
                return ok;
            }
        }

        public ICommand Cancel
        {
            get
            {
                if (cancel == null)
                {
                    cancel = new RelayCommand(DoCancel);
                }
                return cancel;
            }
        }

        public ICommand Apply
        {
            get
            {
                if (apply == null)
                {
                    apply = new RelayCommand(DoApply);
                }
                return apply;
            }
        }

        public ICommand ChangePassword
        {
            get
            {
                if (changePassword == null)
                {
                    changePassword = new RelayCommand(DoChangePassword);
                }
                return changePassword;
            }
        }

        public UserEditVM(IFacade facade, int userId)
        {
            this.facade = facade;
            this.userId = userId;
            this.username = facade.Users[userId].Username;
            this.isActive = facade.Users[userId].IsActive;
        }

        private void DoOK()
        {
            try
            {
                facade.UserUpdate(new UserData(userId, username, isActive));
            }
            catch (Exception ex)
            {
                facade.ShowErrorDialog("An unexpected error occured while saving the user.", ex);
                return;
            }
            facade.CloseWindow(this);
        }

        private void DoCancel()
        {
            facade.CloseWindow(this);
        }

        private void DoApply()
        {
            try
            {
                facade.UserUpdate(new UserData(userId, username, isActive));
            }
            catch (Exception ex)
            {
                facade.ShowErrorDialog("An unexpected error occured while saving the user.", ex);
            }
        }

        private void DoChangePassword()
        {
            facade.ShowUserChangePasswordDialog(userId);
        }
    }
}
